package com.easy.user.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.easy.common.bean.SysRole;
import com.easy.common.enumbean.RoleStatus;
import com.easy.common.service.IRoleService;
import com.easy.user.mapper.RoleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RoleService implements IRoleService {

    @Autowired
    private RoleMapper roleMapper;



    @Override
    public SysRole insert(SysRole sysRole) {
        roleMapper.insert(sysRole);
        return roleMapper.selectById(sysRole.getId());
    }

    @Override
    public SysRole update(SysRole sysRole) {
        roleMapper.updateById(sysRole);
        return roleMapper.selectById(sysRole.getId());
    }

    @Override
    public int delete(Integer id) {
        return roleMapper.deleteById(id);
    }

    @Override
    public SysRole getById(Integer id) {
        return roleMapper.selectById(id);
    }

    @Override
    public IPage<SysRole> getListByPage(IPage page, SysRole sysRole) {
        QueryWrapper<SysRole> queryWrapper = new QueryWrapper<>(sysRole);
        return roleMapper.selectPage(page, queryWrapper);
    }

    @Override
    public List<SysRole> getList(SysRole sysRole) {
        QueryWrapper<SysRole> queryWrapper = new QueryWrapper<>(sysRole);
        return roleMapper.selectList(queryWrapper);
    }

    @Override
    public int setStatus(Integer id) {
        SysRole role = roleMapper.selectById(id);
        if (role != null) {
            RoleStatus currentStatus = role.getStatus();
            RoleStatus newStatus = (currentStatus == RoleStatus.ENABLED) ? RoleStatus.DISABLED : RoleStatus.ENABLED;
            UpdateWrapper<SysRole> updateWrapper = new UpdateWrapper<>();
            updateWrapper.set("status", newStatus).eq("id", id);
            return roleMapper.update(null, updateWrapper);
        }
        return 0;
    }

}